package com.atguigu.result;

import lombok.Data;

@Data
public class Result<T> {
    private  Integer code;
    private String message;
    private T data;

 /*   private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result() {
    }*/

    public static<T> Result<T> build(T data, Integer code, String message){
        Result<T> result=new Result<>();
        if (data!=null){
            result.setData(data);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;

    }
    public static<T> Result<T> build(T data, ResultCodeEnum resultCodeEnum){
        Result<T> result=new Result<>();
        if (data!=null){
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;

    }
    //成功
    public static<T> Result<T> ok(){
        Result<T> result = build(null,ResultCodeEnum.SUCCESS.code,ResultCodeEnum.SUCCESS.message);
        return result;
    }
    public static<T> Result<T> ok(T data){
        Result<T> result = build(data,ResultCodeEnum.SUCCESS.code,ResultCodeEnum.SUCCESS.message);
        return result;
    }
    //失败
    public static<T> Result<T> fail(){
        return build(null,ResultCodeEnum.FAIL.code, ResultCodeEnum.FAIL.message);
    }
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
